import { login, getInfo } from "@/api/login";
import { getToken, setToken, removeToken } from "@/utils/auth";
import { removeSession, setSession, getSession } from "@/utils/support";
import { postAddCartData } from "@/api/product";
const user = {
  state: {
    token: getToken(),
    username: getSession("username"),
    avatar: "",
    cartNum: 0,
    ids: 0
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token;
    },
    SET_NAME: (state, username) => {
      state.username = username;
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar;
    },
    // 加入购物车
    async postAddCartData(state, params = {}) {
      const res = await postAddCartData({
        memberNickname: getSession("username"),
        ...params
      });
      try {
        if (res.code === 200) {
          //mutations.getCartNumData(state);
          state.ids = res.data;
          console.log(state.ids);
          console.log("购物成功");
        }
      } catch (err) {
        console.log(err);
      }
    }
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      const username = userInfo.username.trim();
      const password = userInfo.password.trim();
      return new Promise((resolve, reject) => {
        login(username, password)
          .then(response => {
            const tokenStr = response.data.tokenHead + response.data.token;
            setToken(tokenStr);
            commit("SET_TOKEN", tokenStr);
            setSession("username", username);
            commit("SET_NAME", username);
            resolve();
          })
          .catch(error => {
            reject(error);
          });
      });
    },

    //获取用户信息
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getInfo()
          .then(response => {
            const data = response.data;
            commit("SET_NAME", data.username);
            commit("SET_AVATAR", data.icon);
            resolve(response);
          })
          .catch(error => {
            reject(error);
          });
      });
    },

    // //登出
    // LogOut({ commit, state }) {
    //   return new Promise((resolve, reject) => {
    //     logout(state.token)
    //       .then(() => {
    //         commit("SET_TOKEN", "");
    //         //commit('SET_ROLES', [])
    //         commit("SET_NAME", "");
    //         removeToken();
    //         resolve();
    //       })
    //       .catch(error => {
    //         reject(error);
    //       });
    //   });
    // },

    //前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit("SET_TOKEN", "");
        commit("SET_NAME", "");
        commit("SET_AVATAR", "");
        removeToken();
        removeSession("username");
        localStorage.removeItem("username");
        resolve();
      });
    },
    //添加购物车
    handleAddCart({ commit }, params) {
      return new Promise((resolve, reject) => {
        commit("postAddCartData", params);
        resolve();
      });
    }
  }
};

export default user;
